﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WaveEngine.Framework;

namespace WaveEngine.Extensions.ITasks
{
    class WaitTimeTask : TaskBase
    {
        /// <summary>
        /// The timer
        /// </summary>
        ScopedTimer timer;

        /// <summary>
        /// The duration
        /// </summary>
        TimeSpan duration;

        /// <summary>
        /// Initializes a new instance of the <see cref="WaitTimeTask" /> class.
        /// </summary>
        /// <param name="scene">The scene.</param>
        /// <param name="duration">The duration.</param>
        public WaitTimeTask(Scene scene, TimeSpan duration)
            : base(scene, null)
        {
            this.duration = duration;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="WaitTimeTask" /> class.
        /// </summary>
        /// <param name="parent">The parent.</param>
        /// <param name="duration">The duration.</param>
        public WaitTimeTask(ITask parent, TimeSpan duration)
            : base(parent, "WaitTask" + Guid.NewGuid())
        {
            this.duration = duration;
        }

        /// <summary>
        /// Ons the run.
        /// </summary>
        protected override void performRun()
        {
            this.TaskState = WaveEngine.Framework.Services.TaskState.Running;
            timer = (this.Scene as BaseScene).CreateTimer(this.TaskName, duration, (t) =>
            {
                if (this.TaskState == WaveEngine.Framework.Services.TaskState.Running)
                    this.performCompleted();
                else
                {
#if DEBUG
                    throw new NotSupportedException("This point should be not executed because if it has been skiped or aborted the timer (of the TimerTask)should have been removed");
#endif
                }
            });
        }

        /// <summary>
        /// Notifies the skip.
        /// </summary>
        protected override bool performSkip()
        {
            if (IsSkippable)
            {
                timer.Delete();
                return base.performSkip();
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// Notifies the cancelled.
        /// </summary>
        protected override void performCancel()
        {
            timer.Delete();
            base.performCancel();
        }

        /// <summary>
        /// Returns a <see cref="System.String" /> that represents this instance.
        /// </summary>
        /// <returns>
        /// A <see cref="System.String" /> that represents this instance.
        /// </returns>
        public override string ToString()
        {
            return "WAIT TASK - " + this.duration.ToString();
        }

        /// <summary>
        /// Gets or sets the child tasks.
        /// </summary>
        /// <value>
        /// The child tasks.
        /// </value>
        public override IEnumerable<ITask> ChildTasks
        {
            get { return System.Linq.Enumerable.Empty<ITask>(); }
        }
    }
}
